<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleClose" width="500px">
        <el-form :model="dialogForm" size="small" :rules="rules" ref="dialogFormRef">
            <el-form-item prop="typeName" label="工况类型">
                <el-input v-model="dialogForm.typeName" :disabled="type === 'view'" placeholder="请输入" />
            </el-form-item>
            <el-form-item prop="aircraftType" label="机型">
                <el-select v-model="dialogForm.aircraftType" :disabled="type === 'view'" style="width:100%">
                    <el-option v-for="(item,index) in aircraftTypes" :key="'aircraftType'+index" :label="item.label"
                        :value="item.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="rounds" label="轮次">
                <el-input v-model="dialogForm.rounds" :disabled="type === 'view'" placeholder="请输入" />
            </el-form-item>
            <el-form-item prop="subTypeName" label="子类型">
                <el-input v-model="dialogForm.subTypeName" :disabled="type === 'view'" placeholder="请输入" />
            </el-form-item>
            <el-form-item prop="count" label="数量">
                <el-input-number v-model="dialogForm.count" :disabled="type === 'view'" placeholder="请输入" />
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="handleSure" v-if="type !== 'view'">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { addLoadCaseStatistic, editLoadCaseStatistic } from '@/api/loadCaseStatistics.js'
    export default {
        name: 'loadCaseStatisticsDialog',
        props: {
            type: {
                type: String,
                default: 'add'
            },
            datas: {
                type: Object,
                default: () => ({})
            },
            nodeData: {
                type: Object,
                default: () => ({})
            }
        },
        data() {
            return {
                title: '',
                dialogVisible: true,
                dialogForm: {
                    id: undefined,
                    typeName: '',
                    subTypeName: '',
                    count: '',
                    rounds: '',
                    aircraftType: '',
                    classifyId:null
                },
                rules: {
                    typeName: [{ required: true, message: '类型名称不能为空', trigger: 'blur' }],
                    subTypeName: [{ required: true, message: '子类型名称不能为空', trigger: 'blur' }],
                    count: [{ required: true, message: '计数不能为空', trigger: 'blur' }],
                    aircraftType: [{ required: true, message: '机型不能为空', trigger: 'blur' }],
                    rounds: [{ required: true, message: '轮次不能为空', trigger: 'blur' }]
                },
                aircraftTypes: [
                    {
                        label: 'C909',
                        value: 'C909'
                    },
                    {
                        label: 'C919',
                        value: 'C919'
                    },
                    {
                        label: 'C929',
                        value: 'C929'
                    }
                ]
            }
        },
        mounted() {
            this.initData()
        },
        methods: {
            initData() {
                this.dialogForm = {
                    id: this.datas.id,
                    typeName: this.datas.typeName,
                    subTypeName: this.datas.subTypeName,
                    count: this.datas.count,
                    rounds: this.datas.rounds,
                    aircraftType: this.datas.aircraftType,
                    classifyId:this.nodeData.id
                }
            },
            handleClose() {
                this.$emit('on-close')
            },
            handleSure() {
                this.$refs.dialogFormRef.validate((valid) => {
                    if (valid) {
                        if (this.type === 'edit') {
                            editLoadCaseStatistic(this.dialogForm).then(res => {
                                this.$message.success('编辑成功！')
                                this.$emit('on-sure')
                                this.handleClose()
                            })
                        } else {
                            addLoadCaseStatistic(this.dialogForm).then(res => {
                                this.$message.success('新增成功！')
                                this.$emit('on-sure')
                                this.handleClose()
                            })
                        }
                    } else {
                        this.$message.error('请填写必填信息！')
                        return false;
                    }
                });
            }
        }
    }
</script>

<style scope>
    .symbol-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    .symbol-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    .symbol-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 50px;
        height: 50px;
        line-height: 50px;
        text-align: center;
    }

    .symbol {
        width: 50px;
        height: 50px;
        display: block;
    }
</style>